<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;
use app\admin\model\Cate as CateModel;

class Cate extends Base
{
	public function lst()
	{	
		$list = CateModel::paginate(5);
		$this->assign('list',$list);
	    return $this->fetch('lst');
	}
	
	public function edit()
    {
		$id = input('id');
		$cates = db('cate')->where('id',$id)->find();
		
		if(request()->isPost()){
			$data = [
				'id' =>input('id'),
				'catename'=>input('catename'),
			];
			
			
			// 验证
			$validate = \think\Loader::validate('Cate');
			if(!$validate->check($data)){
			    $this->error($validate->getError());
				die;
			}
			
			if(db('cate')->update($data)){
				$this->success('修改成功！','lst');
			}else{
				$this->error('修改失败！');
			}
			return;
		}
		
		
		$this->assign('cates',$cates);
        return $this->fetch();
    }
    
    public function add()
    {	
		if(request()->isPost()){
			// 接收数据
			$data=[
				'catename'=>input('catename'),
			];
			
			// 验证
			$validate = \think\Loader::validate('Cate');
			if(!$validate->check($data)){
			    $this->error($validate->getError());
				die;
			}
			
			// 判断结果
			if(Db::name('cate')->insert($data)){
				return $this->success('添加栏目成功','lst');
			}else{
				return $this->error('添加栏目失败');
			}
			return;
		}
        return $this->fetch('add');
    }
	
	public function del(){
		$id = input('id');
		if(db('cate')->delete(input('id'))){
			$this->success('删除栏目成功','lst');
		}else{
			$this->error('删除栏目失败');
		}
	}
	
	
}
